package com.ruoyi.web.controller.price;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.IRegionService;
import com.ruoyi.web.domain.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/price/region")
public class RegionController extends BaseController {
    private String prefix = "price/region";

    @Autowired
    private IRegionService regionService;

    @GetMapping("/list")
    public String list(Region po, ModelMap mmap) {
        startPage();
        List<Region> list = regionService.selectRegionList(po);
        TableDataInfo table = getDataTable(list);
        mmap.put("regions", list);
        return prefix + "/list";
    }

    /**
     * 修改角色
     */
    @GetMapping("/edit/{regionId}")
    public String edit(@PathVariable("regionId") Integer regionId, ModelMap mmap){
        mmap.put("region", regionService.selectRegionById(regionId));
        return prefix + "/edit";
    }

    @GetMapping("/delete/{regionId}")
    public String delete(@PathVariable("regionId") Integer regionId, ModelMap mmap){
        mmap.put("region", regionService.deleteRegionById(regionId));
        return prefix + "/edit";
    }

    /**
     * 修改保存角色
     */
    @PostMapping("/edit")
    public String editSave(@Validated Region role) {
        regionService.updateRegion(role);
        return redirect("/price/region/list");
    }

    @GetMapping("/toSave")
    public String toSave(ModelMap mmap){
        return prefix + "/add";
    }

    @PostMapping("/save")
//    @ResponseBody
    public String saveRegion(@Validated Region role) {
         regionService.saveRegion(role);
        return redirect("/price/region/list");
    }
}
